<!--#include file="lib/utf-8.asp"-->
<!--#include file="lib/connopen.asp"-->
<!--#include file="lib/asp-common-lib.asp"-->
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>报关单登记系统</title>
<!--#include file="lib/plugins.asp"-->
<style type="text/css">
	.ui-datepicker table {
		border: 0;
	}
	#tablelist{
		min-width:30%;
	}
	#tmp{
		font-family:宋体;
	}
    .fillspace8{
        width:8em;
        display:inline-block;
    }
    .yform fieldset {
        margin: 0.5em 0.5em;
    }
</style>
<script type="text/javascript">
	$(function() {
        $("#form1").validationEngine('attach',{
            validationEventTrigger:'submit'
        });
        
        var json_fee;
        $.getJSON("json_feecate.asp",function(data){
            json_fee=data;
        });
        function Init_Fee(){
            var selstr="<select class='feeid' name='feeid'>";
            $.each(json_fee,function(i,item){
                selstr+='<option value="'+item.feeid+'">'+item.feename+'</option>';
            });
            selstr+="</select>";
            return selstr;
        }
        function add_feeone_row(){
            var row_str="<tr>";
            row_str+="<td>"+Init_Fee()+"</td>"+
                    "<td><input name='feer' size='4' class='validate[custom[number]]' value='0' /></td>"+
                    "<td><input name='feep' size='4' class='validate[custom[number]]' value='0' /></td>"+
                    "<td><input class='del_fee_row' type='button' value='删除' /></td></tr>";
            return row_str;
        }
            $("#add_fee_row").live('click',function(){
                $(this).closest('tbody').append(add_feeone_row());
            });
            $(".del_fee_row").live('click',function(){
                $(this).closest('tr').remove();
            });
	});
</script>	
<!--#include file="lib/js-google-analytics.asp"-->
</head>
<%
if not CheckPostFlag(session("aaPostFlag"),0,15) then call JumpErrorPage(2)
id=saferequest("id",1)
if id="" then id=0 else id=clng(id)
' tmp=request("tmp")
submit=saferequest("submit",0)
if id<>0 then
	sql = "select * from wtr where id="&id
	Set rs = Server.CreateObject("ADODB.Recordset")
	rs.Open sql,conn,1,1
	if not rs.eof then
		wtr=rs("wtr")
		tmp=rs("tmp")
		wtrpy=rs("wtrpy")
		wtrfull=rs("wtrfull")
		wtrinvoice=rs("wtrinvoice")
		wtrhidden=rs("wtrhidden")
		' salesid=rs("salesid")
	end if
	rs.close
	set rs=nothing
end if
if submit<>"" then
    conn.BeginTrans

	wtr=saferequest("wtr",0)
	tmp=clng(saferequest("tmp",1))
    feeid=saferequest("feeid",0)
    feer=saferequest("feer",0)
    feep=saferequest("feep",0)
    feeids=split(trim(feeid),",")
    feers=split(trim(feer),",")
    feeps=split(trim(feep),",")
	' salesid=clng(saferequest("salesid",1))
	wtrpy=saferequest("wtrpy",0)
	wtrfull=saferequest("wtrfull",0)
	wtrinvoice=saferequest("wtrinvoice",0)
	wtrhidden=saferequest("wtrhidden",0)
    if wtrhidden="" then wtrhidden=false
    roleid=saferequest("roleid",0)
    roleids=split(trim(roleid),",")
    userid=saferequest("userid",0)
    userids=split(trim(userid),",")
    
	if submit="新增" then
		sql="insert into wtr(wtr,tmp,wtrpy,wtrfull,wtrinvoice,wtrhidden) values('"&wtr&"',"&tmp&",'"&wtrpy&"','"&wtrfull&"','"&wtrinvoice&"',"&wtrhidden&")"
        id=ExecuteCommand(sql,true)
	end if
	if submit="保存" then
        sql="update wtr set wtr='"&wtr&"',tmp="&tmp&",wtrpy='"&wtrpy&"',wtrfull='"&wtrfull&"',wtrinvoice='"&wtrinvoice&"',wtrhidden="&wtrhidden&" where id="&id
        call ExecuteCommand(sql,false)
    end if
    if submit="删除" then
        sql="delete * from wtr where id="&id
        call ExecuteCommand(sql,false)
    end if
    call ExecuteCommand("delete * from wtrfeetmp where wtrid="&id,false)
    for i=0 to Ubound(feeids)
        if feers(i)="" then feers(i)=0
        if feeps(i)="" then feeps(i)=0
        if feers(i)<>0 or feeps(i)<>0 then
            sql_addfee="insert into wtrfeetmp(wtrid,feeid,feer,feep) values("&id&","&feeids(i)&","&feers(i)&","&feeps(i)&")"
            call ExecuteCommand(sql_addfee,false)
        end if
    next
    
    call ExecuteCommand("delete * from wtrroleuser where wtrid="&id,false)
    for i=0 to Ubound(roleids)
        if userids(i)<>0 then
            sql_adduser="insert into wtrroleuser(wtrid,roleid,userid) values("&id&","&roleids(i)&","&userids(i)&")"
            call ExecuteCommand(sql_adduser,false)
        end if
    next
    if err.number=0 then 
        conn.CommitTrans  '如果没有conn错误，则执行事务提交
    else
        conn.RollbackTrans '否则回滚
        '回滚后的其他操作
        'strerr=err.Description
        url=request.ServerVariables("http_host")&request.ServerVariables("url")
        Response.Write "数据库错误！<br /><font color=red>Error # " & CStr(Err.Number) & "<br />" & Err.Description & "<br />Source: " & Err.Source &"<br />错误页面："&url&"</font><br />"
        Response.End
    end if
    if submit="新增" then response.redirect("wtr.asp?id="&clng(id))
    if submit="删除" then response.redirect("wtr.asp")

end if
%>

<body>
<!--#include file="lib/header.asp"-->
<!--#include file="lib/nav.asp"-->
<div id="main">
<!--#include file="lib/col1.asp"-->
<!--#include file="lib/col2.asp"-->
	<div id="col3" role="main">
		<div id="col3_content" class="clearfix">
			<form id="form1" name="form1" method="post" class="yform columnar" role="application">
                <div class="subcolumns">
                    <div class="c50l">
                        <fieldset>
                            <legend>委托人信息</legend>
                            <div class="type-text">
                                <label for="wtr">委托人简称*</label>
                                <input name="wtr" id="wtr" type="text" size="10" class="validate[required]" value="<%if submit="新增" then response.write("") else response.write(wtr)%>" />
                            </div>
                            <div class="type-text">
                                <label for="wtrpy">委托人代码(建议用拼音首字母缩写)</label>
                                <input name="wtrpy" id="wtrpy" type="text" size="10" class="validate[custom[onlyLcaseLetter]]" value="<%if submit="新增" then response.write("") else response.write(wtrpy)%>" />
                            </div>
                            <div class="type-text">
                                <label for="wtrfull">委托人全称</label>
                                <input name="wtrfull" id="wtrfull" type="text" size="10"  value="<%if submit="新增" then response.write("") else response.write(wtrfull)%>" />
                            </div>
                            <div class="type-text">
                                <label for="wtrinvoice">委托人发票抬头</label>
                                <input name="wtrinvoice" id="wtrinvoice" type="text" size="10"  value="<%if submit="新增" then response.write("") else response.write(wtrinvoice)%>" />
                            </div>
                            <div class="type-check">
                                <label for="wtrhidden">委托人隐藏</label>
                                <input name="wtrhidden" id="wtrhidden" type="checkbox" value="true" <%if wtrhidden then response.write("checked='checked'")%> />
                            </div>
                            
                            
                            
                            <div class="type-select">
                                <label for="tmp">报关费模板</label>
                                <select name="tmp" id="tmp">
                                    <option value="0">模板　　　　　　应收　　　　　　应付</option>
                                    <%
                                        Set rs_tmp = Server.CreateObject("ADODB.Recordset")
                                        sql = "SELECT * FROM tmp order by id"
                                        rs_tmp.Open sql,conn,1,1
                                        do while not rs_tmp.eof
                                    %>
                                    <option value="<%=rs_tmp("id")%>" <%if rs_tmp("id")=tmp then response.write("selected='selected'")%>><%=FillSpace(rs_tmp("id"),16)&FillSpace(rs_tmp("receive"),16)&rs_tmp("pay")%></option>
                                    <%
                                            rs_tmp.movenext
                                        loop
                                        rs_tmp.close
                                        set rs_tmp=nothing
                                    %>
                                </select>
                            </div>
                        </fieldset>
                        <fieldset>
                            <legend>关联用户角色</legend>
                            <%
                                set rs_role = Server.CreateObject("ADODB.Recordset")
                                sql = "SELECT id,rolename FROM role order by id"
                                rs_role.Open sql,conn,1,1
                                do while not rs_role.eof
                            %>
                            <div class="type-select">
                                <label><%=rs_role("rolename")%></label>
                                <input type="hidden" value="<%=rs_role("id")%>" name="roleid" />
                                <select name="userid">
                                    <option value="0"></option>
                                    <%
                                        set rs_user = Server.CreateObject("ADODB.Recordset")
                                        sql = "select wtrroleuser.userid,fullname,wtrroleuser.userid as wtruser from wtrroleuser,username,role where wtrroleuser.roleid=role.id and wtrroleuser.userid=username.id and wtrroleuser.wtrid="&id&" and wtrroleuser.roleid="&rs_role("id")&" union select userrole.userid,fullname,wtrrole.userid as wtruser from (select * from (select * from userrole where roleid="&rs_role("id")&") as userrole inner join username on userrole.userid=username.id) as userrole left join (SELECT * from wtrroleuser where wtrid="&id&" and roleid="&rs_role("id")&") as wtrrole on userrole.roleid=wtrrole.roleid"
                                        rs_user.Open sql,conn,1,1
                                        do while not rs_user.eof
                                    %>
                                    <option value="<%=rs_user("userid")%>" <%if rs_user("wtruser")=rs_user("userid") then response.write("selected='selected'")%>><%=rs_user("fullname")%></option>
                                    <%
                                            rs_user.movenext
                                        loop
                                        rs_user.close
                                        set rs_user=nothing
                                    
                                    %>
                                </select>
                            </div>
                            <%
                                    rs_role.movenext
                                loop
                                rs_role.close
                                set rs_role=nothing
                            %>
                        </fieldset>
                    </div>
                    <div class="c50r">
                    
                    
                        <table>
                            <thead>
                                <tr><th colspan="4" scope="col">附加费模板</th></tr>
                            </thead>
                            <tbody>
                                <tr>
                                    <th scope="col">附加费项目</th>
                                    <th scope="col">应收</th>
                                    <th scope="col">应付</th>
                                    <th scope="col"><input id="add_fee_row" type="button" value="新增" /></th>
                                    
                                </tr>
                                <%
                                    Set rs_feetmp = Server.CreateObject("ADODB.Recordset")
                                    sql_feetmp = "SELECT * FROM wtrfeetmp where wtrid="&id
                                    rs_feetmp.Open sql_feetmp,conn,1,1
                                    do while not rs_feetmp.eof
                                %>
                                <tr>
                                    <td>
                                        <select class='feeid' name='feeid'>
                                        <%
                                            Set rs_feecate = Server.CreateObject("ADODB.Recordset")
                                            sql_feecate = "SELECT id,feename FROM detail"
                                            rs_feecate.Open sql_feecate,conn,1,1
                                            do while not rs_feecate.eof
                                        %>
                                        <option value="<%=rs_feecate("id")%>" <%if rs_feecate("id")=rs_feetmp("feeid") then response.write("selected='selected'")%>><%=rs_feecate("feename")%></option>
                                        <%
                                                rs_feecate.movenext
                                            loop
                                            rs_feecate.close
                                            set rs_feecate=nothing
                                        %>
                                        </select>
                                    </td>
                                    <td><input name='feer' size='4' class='validate[custom[number]]' value="<%=rs_feetmp("feer")%>" /></td>
                                    <td><input name='feep' size='4' class='validate[custom[number]]' value="<%=rs_feetmp("feep")%>" /></td>
                                    <td><input class='del_fee_row' type='button' value='删除' /></td>
                                    
                                </tr>
                                <%
                                        rs_feetmp.movenext
                                    loop
                                    rs_feetmp.close
                                    set rs_feetmp=nothing
                                %>
                            </tbody>
                        </table>
                            
                    </div>
                </div>
                
                <div class="type-button">
                <%
                    if id<>0 then
                %>
                    <input type="submit" name="submit" class="submit" value="保存" />
                <%
                        if id<>1 and session("sUserName")="admin" then
                %>
                    <input type="submit" name="submit" class="submit" value="删除" />
                
                <%
                        end if
                    else
                %>
                    <input type="submit" name="submit" class="submit" value="新增" />
                <%
                    end if
                %>
                </div>
			</form>
			<table id="tablelist" class="full">
				<thead>
					<tr><th colspan="9" scope="col">委托人明细</th></tr>
				</thead>
				<tbody>
					<tr>
						<th scope="col">ID</th>
						<th scope="col">委托人简称</th>
						<th scope="col">委托人代码</th>
						<th scope="col">委托人全称</th>
						<th scope="col">委托人发票抬头</th>
						<th scope="col">委托人隐藏</th>
						<th scope="col">关联用户角色</th>
						<th scope="col">报关费模板</th>
						<th scope="col">附加费模板</th>
					</tr>
					<%
						sql = "select wtr.*,tmp.pay,tmp.receive from wtr LEFT JOIN tmp ON wtr.tmp = tmp.id order by wtr.wtr"
						Set rs = Server.CreateObject("ADODB.Recordset")
						rs.Open sql,conn,1,1
						do while not rs.eof
					%>
					<tr>
						<th class="sub" scope="row"><%=rs.AbsolutePosition%></th>
						<td><a href="wtr.asp?id=<%=rs("id")%>"><%=rs("wtr")%></a></td>
						<td><%=rs("wtrpy")%></td>
						<td><%=rs("wtrfull")%></td>
						<td><%=rs("wtrinvoice")%></td>
						<td><%if rs("wtrhidden") then response.write("是")%></td>
						<td>
                        <%
                            sql="select role.rolename,username.fullname,wtrroleuser.* from wtrroleuser,username,role where role.id=wtrroleuser.roleid and username.id=wtrroleuser.userid and wtrid="&rs("id")
                            Set rs_user = Server.CreateObject("ADODB.Recordset")
                            rs_user.Open sql,conn,1,1
                            do while not rs_user.eof
                                response.write("<span class='fillspace8'><a href='role.asp?id="&rs_user("roleid")&"'>"&rs_user("rolename")&"</a>:</span><a href='username.asp?id="&rs_user("userid")&"'>"&rs_user("fullname")&"</a><br />")
                                rs_user.movenext
                            loop
                            rs_user.close
                            set rs_user=nothing
                        %>
                        </td>
						<td><%if rs("tmp")<>0 then%><a href="tmp.asp?id=<%=rs("tmp")%>"><%=rs("receive")&"|"&rs("pay")%></a><%end if%></td>
                        <td>
                        <%
                            sql="select feename,feer,feep from wtrfeetmp left join detail on wtrfeetmp.feeid=detail.id where wtrid="&rs("id")
                            Set rs_ic = Server.CreateObject("ADODB.Recordset")
                            rs_ic.Open sql,conn,1,1
                            do while not rs_ic.eof
                                response.write("<span class='fillspace8'>"&rs_ic("feename")&":</span>"&rs_ic("feer")&"|"&rs_ic("feep")&"<br />")
                                rs_ic.movenext
                            loop
                            rs_ic.close
                            set rs_ic=nothing
                        
                        %>
                        </td>
					</tr>
					<%
						
							rs.movenext
						loop
						rs.close
						set rs=nothing
					
					%>
					
				</tbody>
			</table>
		</div>
	</div>
</div>
<!--#include file="lib/footer.asp"-->
</body>
</html>
<!--#include file="lib/connclose.asp"-->

